﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
namespace _1stVATCWin
{
    public partial class Information : Form
    {
        private string userName;
        public Information(string userName)
        {
            InitializeComponent();
            this.userName = userName;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBoxoldPass.Enabled = true;
            textBoxNewPass.Enabled = true;
            textBoxEmail.Enabled = true;
            textBoxAdress.Enabled = true;
            textBoxPhone.Enabled = true;
            textBoxConfimPass.Enabled = true;
        }
        private void load_datagrid(string userName)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            String connectionstring = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select IDnhanvien,email,diachi,sodt from NHANVIEN where IDnhanvien = @IDnhanvien";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@IDnhanvien", userName);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Table NHANVIEN");
            DataTable dt = ds.Tables["Table NHANVIEN"];
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["Table NHANVIEN"].DefaultView;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            load_datagrid(userName);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBoxoldPass.Text ==""&&textBoxNewPass.Text==""&&textBoxConfimPass.Text==""&textBoxEmail.Text==""&&textBoxAdress.Text==""&&textBoxPhone.Text=="")
            {
                errorProviderOld.SetError(textBoxoldPass,"Old Password Not Null");
                errorProviderNewPass.SetError(textBoxNewPass,"New Password Not Null");
                errorProviderConfimpass.SetError(textBoxConfimPass,"Confirm Password Not Null");
                errorProviderAdress.SetError(textBoxAdress,"Adress Not Null");
                errorProviderEmail.SetError(textBoxEmail,"Email Not Null");
                errorProviderPhone.SetError(textBoxPhone,"Email Not Null");
            }
            else
            {
                if(textBoxoldPass.Text =="")
                {
                    errorProviderOld.SetError(textBoxoldPass, "Old Password Not Null");
                }
                else
                {
                    if(textBoxNewPass.Text=="")
                    {
                        errorProviderNewPass.SetError(textBoxNewPass, "New Password Not Null");
                    }
                    else
                    {
                        if(textBoxConfimPass.Text=="")
                        {
                            errorProviderConfimpass.SetError(textBoxConfimPass, "Confirm Password Not Null");
                        }
                        else
                        {
                            if(textBoxEmail.Text=="")
                            {
                                errorProviderEmail.SetError(textBoxEmail, "Email Not Null");
                            }
                            else
                            {
                                if(textBoxAdress.Text=="")
                                {
                                    errorProviderAdress.SetError(textBoxAdress, "Adress Not Null");
                                }
                                else
                                {
                                    if(textBoxPhone.Text=="")
                                    {
                                        errorProviderPhone.SetError(textBoxPhone, "Email Not Null");
                                    }
                                    else
                                    {
                                        if (textBoxNewPass.Text == textBoxConfimPass.Text)
                                        {
                                            if (textBoxNewPass.Text != textBoxoldPass.Text)
                                            {
                                                if (Regex.IsMatch(textBoxEmail.Text, @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$") == false)
                                                {
                                                    errorProviderEmail.SetError(textBoxEmail, "Email khong hop le");
                                                }
                                                else
                                                {
                                                    Config file = new Config(Application.StartupPath + "\\config.ini");
                                                    String connectionString = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
                                                    SqlConnection conn = new SqlConnection(connectionString);
                                                    string sql = "select * from NHANVIEN Where IDnhanvien = @IDnhanvien and matkhau = @matkhau";
                                                    SqlCommand cmd = new SqlCommand(sql, conn);
                                                    cmd.Parameters.AddWithValue("@IDnhanvien", userName);
                                                    cmd.Parameters.AddWithValue("@matkhau", textBoxoldPass.Text);
                                                    conn.Open();
                                                    SqlDataReader reader = cmd.ExecuteReader();
                                                    if (reader.Read())
                                                    {
                                                        reader.Close();
                                                        cmd.CommandText = "update NHANVIEN set IDnhanvien = @IDnhanvien,matkhau=@matkhau,email=@email,diachi=@diachi,sodt=@sodt where IDnhanvien = @IDnhanvien";
                                                        cmd.Parameters.Clear();
                                                        cmd.Parameters.AddWithValue("@IDnhanvien", userName);
                                                        cmd.Parameters.AddWithValue("@matkhau", textBoxNewPass.Text);
                                                        cmd.Parameters.AddWithValue("@email", textBoxEmail.Text);
                                                        cmd.Parameters.AddWithValue("@diachi", textBoxAdress.Text);
                                                        cmd.Parameters.AddWithValue("@sodt", textBoxPhone.Text);
                                                    }
                                                    else
                                                    {
                                                        reader.Close();
                                                        errorProviderOld.SetError(textBoxoldPass, "Password sai, Nhap Lai pass");                                                                                 
                                                    }
                                                    cmd.ExecuteNonQuery();
                                                    conn.Close();
                                                    textBoxoldPass.Text = "";
                                                    textBoxNewPass.Text = "";
                                                    textBoxConfimPass.Text = "";
                                                    textBoxEmail.Text = "";
                                                    textBoxAdress.Text = "";
                                                    textBoxPhone.Text = "";
                                                }  
                                            }
                                            else
                                            {
                                                errorProviderNewPass.SetError(textBoxNewPass, "Khong duoc trung pass old");
                                            }
                                        }
                                        else
                                        {
                                            errorProviderConfimpass.SetError(textBoxConfimPass, "Password khong trung khop");                                                                             
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        private void Load_Update(string idnhanvien)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            String connectionString = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from NHANVIEN Where IDnhanvien = @IDnhanvien";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@IDnhanvien", idnhanvien);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if(reader.Read())
            {
                textBoxEmail.Text = reader["email"].ToString();
                textBoxAdress.Text = reader["diachi"].ToString();
                textBoxPhone.Text = reader["sodt"].ToString();
            }
            reader.Close();
            conn.Close();
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            String idnhanvien = dataGridView1.Rows[e.RowIndex].Cells["IDnhanvien"].Value.ToString();
            Load_Update(idnhanvien);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
